package com.wuliupai.delivery.wuliupaidelivery.activity;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.adapter.walletTradingRecordAdapter;
import com.wuliupai.delivery.wuliupaidelivery.bean.WalletEntity;
import com.wuliupai.delivery.wuliupaidelivery.constants.CommConstants;
import com.wuliupai.delivery.wuliupaidelivery.constants.Value;
import com.wuliupai.delivery.wuliupaidelivery.net.NetResultOperate;
import com.wuliupai.delivery.wuliupaidelivery.ui.service.StickyListHeadersListView;
import com.wuliupai.delivery.wuliupaidelivery.utils.JsonUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.LogUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * 钱包交易记录界面
 */
@EActivity(R.layout.activity_wallet_trading_record)
public class WalletTradingRecordActivity extends AppBaseActivity implements StickyListHeadersListView.OnLoadingMoreLinstener, AppBaseActivity.OnRefreshBtnClickListener {

    @ViewById(R.id.lv_wallet_record_StickyListHeadersListView)
    StickyListHeadersListView lv_wallet_record_StickyListHeadersListView;

    List<WalletEntity.InfolistBean> infolist;

    private Dialog progressDialog; //加载动画
    private LayoutInflater inflater;
    private RelativeLayout moredata;
    private View progressBarView;
    private AnimationDrawable loadingAnimation;
    private TextView progressBarTextView;
    private walletTradingRecordAdapter adapterInsurance;
    private boolean isLoading = false;
    private int startPosition = 0;
    private List<WalletEntity.InfolistBean> listInsurance;
    private int COMMON_PAGE_SIZE = 20;


    @AfterViews
    void startViews() {

        setDefaultTitle(this, "收支记录");
        LogUtils.log("aaa" + ":a");
        //initData();
        initView();
        initInsurance();
        initAdapter();
    }

    @Override
    protected void onRestart() {
        listInsurance.clear();
        initInsurance();
        super.onRestart();
    }

    /**
     * 初始化适配器
     */
    private void initAdapter() {
        listInsurance = new ArrayList<>();
        adapterInsurance = new walletTradingRecordAdapter(WalletTradingRecordActivity.this, listInsurance);
        lv_wallet_record_StickyListHeadersListView.setAdapter(adapterInsurance);
    }

    @UiThread()
    void dismissDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    @UiThread()
    void hideProgress() {
        /*progressBarView.setVisibility(View.VISIBLE);
        progressBarTextView.setVisibility(View.VISIBLE);*/
    }

    /**
     * 初始化控件
     */
    private void initView() {
        inflater = LayoutInflater.from(WalletTradingRecordActivity.this);
        moredata = (RelativeLayout) inflater.inflate(R.layout.moredata, null);

        progressBarView = (View) moredata
                .findViewById(R.id.loadmore_foot_progressbar);
        progressBarTextView = (TextView) moredata
                .findViewById(R.id.loadmore_foot_text);

        loadingAnimation = (AnimationDrawable) progressBarView.getBackground();
        lv_wallet_record_StickyListHeadersListView.addFooterView(moredata);
        lv_wallet_record_StickyListHeadersListView.setAdapter(adapterInsurance);
        lv_wallet_record_StickyListHeadersListView.setLoadingMoreListener(this);
    }

    @UiThread
    void refreshDate() {
        loadingFinished();
    }

    private void loadingFinished() {

        if (null != loadingAnimation && loadingAnimation.isRunning()) {
            loadingAnimation.stop();
        }
        progressBarView.setVisibility(View.INVISIBLE);
        progressBarTextView.setVisibility(View.INVISIBLE);
        isLoading = false;
        adapterInsurance.notifyDataSetChanged();
    }

    @Override
    public void OnLoadingMore() {

        progressBarView.setVisibility(View.VISIBLE);
        progressBarTextView.setVisibility(View.VISIBLE);

        loadingAnimation.start();
        if (!isLoading) {
            isLoading = true;
            startPosition += COMMON_PAGE_SIZE;
            initInsurance();
        }
    }

    /**
     * 初始化数据
     */
    private void initInsurance() {
       /* if (null == progressDialog) {
            progressDialog = new Dialog(WalletTradingRecordActivity.this, R.style.dialog);
        }
        dialogUtils.showLoadingDialog(WalletTradingRecordActivity.this, progressDialog);

        RequestCallback insuranceRequest = new AbstractRequestCallback(WalletTradingRecordActivity.this, progressDialog) {
            @Override
            public void onSuccess(String content) {
                WalletEntity walletEntity = JsonUtil.parseWallet(content);

                if (walletEntity != null) {
                    listInsurance.addAll(walletEntity.getInfolist());
                    dismissDialog();
                    refreshDate();
                }
            }

            @Override
            public void onNoMoreData(String errorMessage) {
                dismissDialog();
                refreshDate();
                WuliupaiApplication.toast(errorMessage);
            }

            @Override
            public void onNoData(String errorMessage) {
                dismissDialog();
                super.onNoData(errorMessage);
            }

            @Override
            public void onFail(String errorMessage) {
                dismissDialog();
                super.onFail(errorMessage);
            }

        };

        String aesBillList = null;
        WalletEntity billList = new WalletEntity(startPosition, COMMON_PAGE_SIZE);
        String jsonBillList = JsonUtil.createJsonString(billList);
        try {
            aesBillList = JsonUtil.aes(jsonBillList);

        } catch (Exception e) {
            e.printStackTrace();
        }
        ConnectUtil.connect(WalletTradingRecordActivity.this, insuranceRequest, aesBillList, "getusermoneyinfo");
        */
        WalletEntity billList = new WalletEntity(this, startPosition, Value.listCount);
        String jsonBillList = JsonUtils.createJsonString(billList);
        LogUtils.log("收支列表send---" + jsonBillList);
        try {
            String aes = JsonUtils.aes(jsonBillList);
            callNet(this, "getusermoneyinfo", aes, new NetResultOperate() {
                @Override
                public void onSuccess(String content) {
                WalletEntity walletEntity = (WalletEntity) JsonUtils.parseJsonString(content,WalletEntity.class);
                if (walletEntity != null) {
                    listInsurance.addAll(walletEntity.getInfolist());
                    dismissDialog();
                    refreshDate();
                }
                    LogUtils.log("收支列表receive---" + content);
                }

                @Override
                public void onErr(String errorCode, String errorMsg, Context mContext) {
                    super.onErr(errorCode, errorMsg, mContext);
                    setCommenActivtiyType(CommConstants.TYPE_ERROR, WalletTradingRecordActivity.this);
                    LogUtils.log("aaa2 " + errorCode + " aa " + errorMsg);
                }

                @Override
                public void onDataEmpty(String content) {
                    setCommenActivtiyType(CommConstants.TYPE_EMPTY, WalletTradingRecordActivity.this);
                    LogUtils.log("aaa3" + content);
                }

                @Override
                public void onDataEnd(String content) {
                    LogUtils.log("aaa4" + content);
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 点击刷新
     */
    @Override
    public void refreshClick() {
        initInsurance();
    }
}
